package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.wordmemorize.common.aspect.operate.OperateLog;
import com.cskaoyan.wordmemorize.converter.LevelConverter;
import com.cskaoyan.wordmemorize.dao.entity.LevelPrivilegeDO;
import com.cskaoyan.wordmemorize.dao.mapper.LevelPrivilegeMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteLevelPrivilegeOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.LevelPrivilegeDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.LevelPrivilegeCommand;
import com.cskaoyan.wordmemorize.request.LevelPrivilegePageRequest;
import com.cskaoyan.wordmemorize.service.LevelPrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LevelPrivilegeServiceImpl implements LevelPrivilegeService {
    @Autowired
    LevelConverter levelConverter;

    @Autowired
    LevelPrivilegeMapper levelPrivilegeMapper;

    /**
     * 等级特权列表
     * @param pageRequest
     * @return
     */
    @Override
    public PageDTO<LevelPrivilegeDTO> getLevelPrivilegeList(LevelPrivilegePageRequest pageRequest) {

        Page<LevelPrivilegeDO> pageParam = new Page<>(pageRequest.getPageNum(), pageRequest.getCount());
        LambdaQueryWrapper<LevelPrivilegeDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (pageRequest.getLevelId() != null) {
            lambdaQueryWrapper.eq(LevelPrivilegeDO::getLevelId, pageRequest.getLevelId());
        }
        if (pageRequest.getKeyword() != null) {
            lambdaQueryWrapper.like(LevelPrivilegeDO::getPrivilegeCode, pageRequest.getKeyword())
                    .or()
                    .like(LevelPrivilegeDO::getPrivilegeName, pageRequest.getKeyword());
        }
        Page<LevelPrivilegeDO> levelPrivilegeDOPage = levelPrivilegeMapper.selectPage(pageParam, lambdaQueryWrapper);

        return levelConverter.levelPrivilegeDOToPageDTO(levelPrivilegeDOPage.getRecords(), (int) levelPrivilegeDOPage.getTotal());
    }

    /**
     * 添加等级特权
     * @param command
     */
    @Override
    @OperateLog(dataType = "等级特权", operateType = 1, description = "新增等级特权")
    public void addLevelPrivilege(LevelPrivilegeCommand command) {
        LevelPrivilegeDO levelPrivilegeDO = levelConverter.levelPrivilegeCommandToLevelPrivilegeDO(command);

        levelPrivilegeMapper.insert(levelPrivilegeDO);
    }

    /**
     * 更新等级特权
     * @param command
     */
    @Override
    @OperateLog(dataType = "等级特权", operateType = 4, description = "修改等级特权")
    public void updateLevelPrivilege(LevelPrivilegeCommand command) {

        LambdaUpdateWrapper<LevelPrivilegeDO> levelPrivilegeDOLambdaUpdateWrapper = new LambdaUpdateWrapper<>();

        LevelPrivilegeDO levelPrivilegeDO = levelConverter.levelPrivilegeCommandToLevelPrivilegeDO(command);

        levelPrivilegeDOLambdaUpdateWrapper.eq(LevelPrivilegeDO::getId, levelPrivilegeDO.getId());

        levelPrivilegeMapper.update(levelPrivilegeDO, levelPrivilegeDOLambdaUpdateWrapper);
    }

    /**
     * 删除等级特权
     * @param id
     * @return
     */
    @Override
    @OperateLog(dataType = "等级特权", operateType = 2, description = "删除等级特权", logReturnValue = true)
    public DeleteLevelPrivilegeOperateDTO deleteLevelPrivilege(Long id) {

        LevelPrivilegeDO levelPrivilegeDO = levelPrivilegeMapper.selectById(id);


        DeleteLevelPrivilegeOperateDTO deleteLevelPrivilegeOperateDTO = levelConverter.levelPrivilegeDOToDeleteLevelPrivilegeOperateDTO(levelPrivilegeDO);

        levelPrivilegeMapper.deleteById(id);

        return deleteLevelPrivilegeOperateDTO;
    }

    /**
     * 批量删除等级特权
     * @param command
     * @return
     */
    @Override
    @OperateLog(dataType = "等级特权", operateType = 3, description = "批量删除等级特权", logReturnValue = true)
    public List<DeleteLevelPrivilegeOperateDTO> batchDeleteLevelPrivilege(BatchDeleteCommand command) {

        List<LevelPrivilegeDO> levelPrivilegeDOS = levelPrivilegeMapper.selectByIds(command.getIds());

        List<DeleteLevelPrivilegeOperateDTO> deleteLevelPrivilegeOperateDTOS = levelConverter.levelPrivilegeDOsToDeleteLevelPrivilegeOperateDTOs(levelPrivilegeDOS);

        levelPrivilegeMapper.deleteByIds(command.getIds());

        return deleteLevelPrivilegeOperateDTOS;
    }
}
